/*
 *  Copyright 2012 Dirk Vranckaert
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package eu.vranckaert.vvl.activities.list.adapter;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import eu.vranckaert.vvl.R;
import eu.vranckaert.vvl.model.TrafficInfoText;
import eu.vranckaert.vvl.model.TrafficInfoTextEnum;

import java.util.Arrays;
import java.util.List;

/**
 * User: DIRK VRANCKAERT
 * Date: 17/02/12
 * Time: 11:44
 */
public class TrafficInfoTextAdapter extends ArrayAdapter<TrafficInfoText> {
    private static final String LOG_TAG = TrafficInfoTextAdapter.class.getSimpleName();
    
    private Activity ctx;
    private List<TrafficInfoText> items;
    private int layoutId;

    /**
     * {@inheritDoc}
     */
    public TrafficInfoTextAdapter(Activity ctx, List<TrafficInfoText> items, int layoutId) {
        super(ctx, layoutId, items);

        this.layoutId = layoutId;
        this.ctx = ctx;
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(LOG_TAG, "Start rendering/recycling row " + position);
        View row;

        if (convertView == null) {
            Log.d(LOG_TAG, "Render a new line in the list");
            row = ctx.getLayoutInflater().inflate(layoutId, parent, false);
        } else {
            Log.d(LOG_TAG, "Recycling an existing line in the list");
            row = convertView;
        }
        
        TrafficInfoText item = items.get(position);

        TextView title = (TextView) row.findViewById(R.id.list_item_traffic_info_text_title);
        TextView type = (TextView) row.findViewById(R.id.list_item_traffic_info_text_type);
        TextView lastUpdateTimestamp = (TextView) row.findViewById(R.id.list_item_traffic_info_text_last_update_timestamp);

        List<TrafficInfoTextEnum.InformationType> typeValues = Arrays.asList(TrafficInfoTextEnum.InformationType.values());
        int typeIndex = typeValues.indexOf(item.getInformationType());
        
        title.setText(item.getTitle());
        type.setText(ctx.getResources().getTextArray(R.array.traffic_info_text_criteria_spinner_values_information)[typeIndex]);

        Log.d(LOG_TAG, "Done rendering row " + position);
        return row;
    }

    public void refill(List<TrafficInfoText> items) {
        this.items.clear();
        this.items.addAll(items);
        notifyDataSetChanged();
    }
}
